package com.my.test;

import java.text.DecimalFormat;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.BiConsumer;

import com.my.utils.DateTime;
import com.my.utils.StrBuilder;

//多次执行的任务统计结果
public class ExecResult {

	private CalculateTask task;
	// 执行次数耗时
	private Map<Integer, Long> map = new TreeMap();

	public ExecResult(CalculateTask task) {
		this.task = task;
	}

	// 统计执行结果
	public void addResult(int count, long nanoConsume) {
		map.put(count, nanoConsume);
	}

	public String getResult() {
		final StrBuilder sb = new StrBuilder();
		sb.appendLine("---------" + task.getName() + "-------------");
		map.forEach((Integer t1, Long t2) -> {
			String t1Str = fromat(t1);
			sb.appendLine("执行" + t1Str + "次:" + DateTime.toMs(t2) + "(ms)");
		});
		return sb.toString();
	}

	public static String fromat(int value) {
		DecimalFormat df = new DecimalFormat();
		df.applyPattern(",###.##");// 将格式应用于格式化器
		return df.format(value);

	}

}
